package com.rmdjzz.slmmn.controller.admin.commentManagement;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rmdjzz.slmmn.pojo.base.Comment;
import com.rmdjzz.slmmn.pojo.base.Song;
import com.rmdjzz.slmmn.pojo.result.Result;
import com.rmdjzz.slmmn.pojo.vo.CommentIdsVo;
import com.rmdjzz.slmmn.pojo.vo.CommentVO;
import com.rmdjzz.slmmn.pojo.vo.PageVo;
import com.rmdjzz.slmmn.service.CommentService;
import com.rmdjzz.slmmn.service.SongService;
import com.rmdjzz.slmmn.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


/**
 * @作者 张绍鑫
 * @创建时间 2023/5/27
 */
@Controller
@RequestMapping("/admin")
@Api(tags = "评论管理api文档")
@Slf4j
public class CommentController{
    @Autowired
    private CommentService commentService;
    @Autowired
    private UserService userService;
    @Autowired
    private SongService songService;

    @ApiOperation(value = "评论分页")
    @GetMapping("/commentPage")
    @ResponseBody
    public Result commentPage(PageVo pageVo){
        //歌曲集合-遍历音乐id，查询到评论集合，遍历评论集合，转为vo
        //获取歌曲数据
        log.info("pageVo:" + pageVo);
        List<Song> records;
        if (pageVo.getStr()!=null && !pageVo.getStr().equals("")){
            log.info("存在模糊查询");
            QueryWrapper<Song> qw=new QueryWrapper<>();
            qw.like("song_name",pageVo.getStr());
            records = songService.list(qw);
        }else {
            log.info("不存在模糊查询");
            records = songService.list();
        }
        log.info("songList:"+records);
        //拿到歌曲id集合
        ArrayList<Object> ids = new ArrayList<>();
        for (Song song: records){
            ids.add(song.getId());
        }
        //通过id集合分页查询评论表
        QueryWrapper<Comment> commentQueryWrapperIds = new QueryWrapper<>();
        commentQueryWrapperIds.in("song_id",ids);

        Page objectPage = new Page<>(pageVo.getStartIndex(),pageVo.getSize());
        Page commentPage = commentService.page(objectPage, commentQueryWrapperIds);
        //获取评论列表
        List<Comment> commentRecords = commentPage.getRecords();
        List<CommentVO> commentVOS=new ArrayList<>();
        //改造page，将comment的vo封装给commentPage
        for (Comment comment : commentRecords){
            System.out.println(comment);
            CommentVO commentVO = new CommentVO();
            commentVO.setId(comment.getId());
            if (userService.getById(comment.getUserId())==null) {
                commentVO.setUser("未知用户");
            }else {
                commentVO.setUser(userService.getById(comment.getUserId()).getUserName());
            }
            commentVO.setSong(songService.getById(comment.getSongId()).getSongName());
            commentVO.setComment(comment.getComment());
            commentVO.setCreateTime(comment.getCreateTime());
            commentVOS.add(commentVO);
        }
        commentPage.setRecords(commentVOS);
        return Result.ok(pageVo.getStr(),commentPage);
    }

    @ApiOperation(value = "根据id删除评论")
    @DeleteMapping("/commentDel/{id}")
    @ResponseBody
    public Result commentDel(@PathVariable Integer id){
        boolean b = commentService.removeById(id);
        return Result.ok(b);
    }
    @ApiOperation(value = "根据id删除批量评论")
    @DeleteMapping("/commentDelIds")
    @ResponseBody
    public Result commentDelIds(CommentIdsVo ids){
        return Result.ok(commentService.removeBatchByIds(ids.getIds()));
    }

}
